

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class DetectURLEncoding {
	static Pattern utf8EncString = Pattern.compile("(?<=[-=])?(?<!%[a-fA-F0-9][a-fA-F0-9])(%[Ee][^& -]*)(?=%[a-fA-F0-9][a-fA-F0-9])(?=\")?");
	static Pattern gbkEncString = Pattern.compile("(?<=[-=])?(?<!%[a-fA-F0-9][a-fA-F0-9])(%[^Ee][^& -]*)(?=%[a-fA-F0-9][a-fA-F0-9])(?=\")?");
	
	String Detect (String s) {
		Matcher matcher_utf8 = utf8EncString.matcher(s);
		Matcher matcher_gbk = gbkEncString.matcher(s);
		if (matcher_utf8.find()){
			return "utf8";
		}else if (matcher_gbk.find()){
			return "gbk";
		}
		return "utf8";
	}
	boolean isEncStr (String s) {
		Matcher matcher_utf8 = utf8EncString.matcher(s);
		Matcher matcher_gbk = gbkEncString.matcher(s);
		if (matcher_utf8.find()) {return true;}
		else if (matcher_gbk.find()) {return true;}
		else {return false;}
	}
}
